define([
    'Util',
    'dialog',
    'tab',
    'cookie',
    'text!tpl/project/viewProject.tpl',
    '../common/getPath'
],function(Util, Dialog, Tab, $cookie, viewProjectTpl, AjaxPath){
    require(['style!css/style.css']);
    //定义jquery类型的元素$el
    var $el=null,
        viewProject = null,
        newInput = null,
        getResult = null;
    //定义初始化函数，程序从这里开始执行
    var initialize = function(index, options, tabItem){
         $el = $(viewProjectTpl);
        //当选项卡处于激活状态时触发
        tabItem.on("tabActive",function(){
            tabItem.show(true); //刷新表单
        });
        //console.log(options);
        
        viewProject = tabItem;
        this.content = $el;
        tplId = options.menuId;
        getProjectView('');
        eventInit();
       /* http://192.168.91.172:8089/devops-project/myProjects?userId=16*/
       /*http://192.168.91.172:8089/devops-project/myProjects?userId=16&projectName=dev*/
    };
    /*重置*/
    var viewProjectReset=function () {
        $('.newInput',$el).val("");
        getProjectView($('input[name=searchProject]', $el).val().trim());
    };

    /*跳转到需求列表*/
    var viewTabRequireList=function (item) {
        $cookie.set("projectSelectId", $(this).attr("dataId"));
        $cookie.set("projectTagName", $(this).attr("dataTagName"));
        /*viewProject.createTab({
            title: '工作项',
            closeable: 1, //是否可以关闭 0false不可以|1true可以
            className: 'requireListTab'
        });
        viewProject.switchTab('工作项');
        //console.log($(_this).attr("dataId"));
        var objProject={}; 
        objProject.menuId = tplId;
        require(['js/requirement/requireList'], function(requireList) {
            var result = new requireList({}, objProject, viewProject);
            //console.log(objProject);
            viewProject.content(result.content);
        });*/
        $(".nav .menu li a[data-id=7]").click();
    };
    //dom元素事件初始化
    var eventInit = function(){
        //为搜索按钮注册事件
        $el.on('click', '.newSearch', function(){
            getProjectView($('input[name=searchProject]', $el).val().trim());
        });//查询
        $el.on('click', '.newReset',viewProjectReset);//重置
        $el.on('click', '.project_btn',viewTabRequireList);/*跳转到需求列表*/
        $('.newInput', $el).on("keypress",function(e){
            if(e.keyCode === 13){
                e.preventDefault();
            }
        });
             
        $('#tabList li.' + tplId).on('click', tabActive);
    };
    var tabActive = function() {
        getProjectView( $('input[name=searchProject]', $el).val().trim() );
    };
/*"+ $cookie.get("userId")+"*/
    var getProjectView=function (name) {
        Util.ajax.getJson(AjaxPath.project+"/myProjects?userId="+ $cookie.get("userId")+"&projectName=" + name , {}, function(result, isok) {
                if(result.returnCode==="0000"){
                    var viewProjectTitle ='';
                    getResult=result.beans;
                    //console.log(result);
                    if(result.bean.total===0){
                        var  NothingConfig = {
                            mode: 'confirm', // 对话框模式，默认normal标准|confirm浮动层|confirm确认对话框
                            title:'提示',
                            width: 300, //对话框宽度，可选项。normal默认值为600，confirm默认值为300，tips默认值为240
                            height: 120, //对话框高度，可选项。normal默认值为400，confirm默认值为180，tips默认值为80
                            cancelDisplay: false, //是否显示取消按钮（强制执行，可在confirm模式下生效），可选项。 默认true(显示)
                            modal:true,
                            content: '<div style="width:90%;word-break:break-all;" class="mt-20 ml-20 fn-left">'+result.returnMessage+'</div>' // 对话框内容，可以是字符串、html片段、或dom对象,默认为loading状态（点组成的圈）
                        };
                        new Dialog(NothingConfig);
                    }
                    for ( var i=0; i<getResult.length;i++){
                            viewProjectTitle += '<li class="projectLi" style="display: block;margin: 10px; border-radius: 10px;">' +
                                '<a href="javascript:;" dataTagName="'+getResult[i].tagName+'" dataStatus="'+getResult[i].projectStatus +'" dataId="'+getResult[i].id+'" class="project_btn"> ' +
                                '<div class="project_title">  ' +
                                '<div class="project_title_name">' +
                                '<div class="viewProjectName" title="'+getResult[i].projectName+'">' +
                                '<em></em> ' +
                                '<span class="projectNameOverflow whiteSpace" style="width:120px">'+getResult[i].projectName+'</span>' +
                                '</div>' +
                                '</div>' +
                                '<div class="project_title_text">' +
                                ' <div class="project_title_text_name">' +
                                '<span>'+getResult[i].userName+'</span>' +
                                '<span>'+getResult[i].projectStatusName +'</span> ' +
                                '<span style="border:none;">'+getResult[i].userCount +'</span>' +
                                '</div>' +
                                '</div>' +
                                '</div>' +
                                '</a>' +
                                '</li>';
                        }
                        $('.ulTitle',$el).html(viewProjectTitle);
                }else {
                  var  config = {
                        mode: 'confirm', // 对话框模式，默认normal标准|confirm浮动层|confirm确认对话框
                        title:'提示',
                        width: 300, //对话框宽度，可选项。normal默认值为600，confirm默认值为300，tips默认值为240
                        height: 120, //对话框高度，可选项。normal默认值为400，confirm默认值为180，tips默认值为80
                        cancelDisplay: false, //是否显示取消按钮（强制执行，可在confirm模式下生效），可选项。 默认true(显示)
                        modal:true,
                        content: '<div style="width:90%;word-break:break-all;" class="mt-20 ml-20 fn-center">'+result.returnMessage+'</div>' // 对话框内容，可以是字符串、html片段、或dom对象,默认为loading状态（点组成的圈）
                    };
                    new Dialog(config);
                }

        });
    };
    //将初始化函数返回
    return initialize;
});
